package com.zhiyun.vo;

import cn.hutool.json.JSONUtil;
import com.zhiyun.domain.TestQuestion;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zc
 * @Date 2025/5/6 下午9:10
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExaminationResponseVo implements Serializable {
    @Serial
    private static final long serialVersionUID = 5201369206038941946L;
//    题目对象
    private TestQuestion testQuestion;
//    我的答案 json格式
    private String answerContent;
//    是否正确
    public Integer getScore(){
        String answer = testQuestion.getAnswer();
        List<String> trueAnswer = JSONUtil.parseArray(answer).toList(String.class);
        List<String> myAnswer = JSONUtil.parseArray(answerContent).toList(String.class);
        //单选题
        if (testQuestion.getTypeId() == 1){
            if (myAnswer.get(0).equals(trueAnswer.get(0))){
                return testQuestion.getScore();
            }
            return 0;
        //多选题
        }else if (testQuestion.getTypeId() == 2){
            //如果正确选项中没有我的选项的一个选项,0分
            for (String s : myAnswer) {
                if (!trueAnswer.contains(s)){
                    return 0;
                }
            }
            //在此时没有退出说明我的选项全都在正确选项中
            int num = 0;
            for (String s : trueAnswer) {
                if (myAnswer.contains(s)){
                    num++;
                }
            }
           //如果我的选项正确的个数和正确选项个数相同,得全部分
            if (num == trueAnswer.size()){
                return testQuestion.getScore();
            }else {
                //如果我的选项只有正确选项的一部分,得一半分
                return testQuestion.getScore()/2;
            }
        //填空题
        }else if (testQuestion.getTypeId() == 3){
            if (myAnswer.get(0).equals(trueAnswer.get(0))){
                return testQuestion.getScore();
            }
            return 0;
        //简答题
        }else if (testQuestion.getTypeId() == 4){
            return 0;
        }
        return 0;
    }
}
